

public class Solution83 {

    public static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

    public ListNode deleteDuplicates(ListNode head) {
        
        if(head == null) return null;
        
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;
        ListNode slow = head;
        ListNode fast = head;

        while(fast != null){
            if(slow.val != fast.val){
                //slow挪位置
                // slow = slow.next;
                //fast赋值给slow()
                // slow=fast;
                slow.next = fast;
                slow = slow.next;
            }
            fast = fast.next;
        }
        //若最后有重复元素需要断裂联系，没有重复元素时slow为最后一个不需要。
        slow.next = null;
        return dummyHead.next;
    }
}